package com.eedi.framework.dept.controller.org;

import com.eedi.framework.dept.controller.org.vo.*;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.dept.controller.org.vo.*;
import com.eedi.framework.dept.dal.dataobject.OrgDeptDO;
import com.eedi.framework.dept.service.OrgDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "经营管理 - 部门")
@RestController
@RequestMapping("/dept")
@Validated
public class OrgDeptController {

    @Resource
    private OrgDeptService orgDeptService;

    @PostMapping("/create")
    @Operation(summary = "创建部门(org:dept:create)")
    @PreAuthorize("@org.hasPermission('org:dept:create')")
    public CommonResult<String> createDept(@Valid @RequestBody OrgDeptSaveReq createReqVO) {
        String deptId = orgDeptService.createDept(createReqVO);
        return success(deptId);
    }

    @PutMapping("/update")
    @Operation(summary = "更新部门(org:dept:update)")
    @PreAuthorize("@org.hasPermission('org:dept:update')")
    public CommonResult<Boolean> updateDept(@Valid @RequestBody OrgDeptUpReq updateReqVO) {
        orgDeptService.updateDept(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除部门(org:dept:delete)")
    @Parameter(name = "orgDeptId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@org.hasPermission('org:dept:delete')")
    public CommonResult<Boolean> deleteDept(@RequestParam("orgDeptId") String orgDeptId) {
        orgDeptService.deleteDept(orgDeptId);
        return success(true);
    }

    @PutMapping("/disable")
    @Operation(summary = "停用部门(org:dept:update)")
    @Parameter(name = "orgDeptId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@org.hasPermission('org:dept:update')")
    public CommonResult<Boolean> disable(@RequestParam("orgDeptId") String orgDeptId) {
        orgDeptService.disable(orgDeptId);
        return success(true);
    }

    @PutMapping("/enable")
    @Operation(summary = "启用部门(org:dept:update)")
    @Parameter(name = "orgDeptId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@org.hasPermission('org:dept:update')")
    public CommonResult<Boolean> enable(@RequestParam("orgDeptId") String orgDeptId) {
        orgDeptService.enable(orgDeptId);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获取部门列表(org:dept:list)")
    @PreAuthorize("@org.hasPermission('org:dept:list')")
    public CommonResult<List<OrgDeptResp>> getDeptList(OrgDeptListReq reqVO) {
        List<OrgDeptDO> list = orgDeptService.getDeptList(reqVO);
        return success(BeanUtils.toBean(list, OrgDeptResp.class));
    }

    @GetMapping(value = {"/list-all-simple"})
    @Operation(summary = "获取部门精简信息列表", description = "只包含被开启的部门，主要用于前端的下拉选项")
    public CommonResult<List<OrgDeptSimpleResp>> getSimpleDeptList() {
        OrgDeptListReq deptListReq = new OrgDeptListReq();
        deptListReq.setOrgDeptStatus(CommonStatusEnum.ENABLE);
        List<OrgDeptDO> list = orgDeptService.getDeptList(deptListReq);
        return success(BeanUtils.toBean(list, OrgDeptSimpleResp.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得部门信息(org:dept:list)")
    @Parameter(name = "orgDeptId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@org.hasPermission('org:dept:list')")
    public CommonResult<OrgDeptResp> getDept(@RequestParam("orgDeptId") String orgDeptId) {
        OrgDeptDO dept = orgDeptService.getDept(orgDeptId);
        return success(BeanUtils.toBean(dept, OrgDeptResp.class));
    }
    @GetMapping("/count")
    @Operation(summary = "获得部门数量(org:dept:list)")
    @PreAuthorize("@org.hasPermission('org:dept:list')")
    public CommonResult<Long> countOrgDept() {
        return success(orgDeptService.countOrgDept());
    }

    @GetMapping("/page")
    @Operation(summary = "获取部门分页列表(org:dept:list)")
    @PreAuthorize("@org.hasPermission('org:dept:list')")
    public CommonResult<PageResult<OrgDeptResp>> getDeptPage(OrgDeptPageReq reqVO) {
        PageResult<OrgDeptDO> page = orgDeptService.getDeptPage(reqVO);
        return success(BeanUtils.toBean(page, OrgDeptResp.class));
    }

}
